//
//  UIDeviceExtension.swift
//  Common
//
//  Created by 我骑着蜗牛走天下 on 2025/3/21.
//

import UIKit

extension UIDevice {
    
    // 判断是否为全面屏
    static func isFullScreen() -> Bool{
        if #available(iOS 11.0, *) {
           // 获取当前窗口的安全区域
           guard let window = UIApplication.shared.windows.first(where: { $0.isKeyWindow }) else { return false }
           return window.safeAreaInsets.bottom > 0
        } else {
           return false // iOS 11 以下无全面屏
        }
    }
    
    /// 判断设备是否为 iPhone X  及以上的机型
    /// 判断是否为全面屏，不包括 SE 机型
    static func isiPhoneXOrNewer() -> Bool {
        guard let info = UIDevice().getDeviceGeneration() else { return false }
        let iphnex = (info.generation == 10 && info.variant == 3) || (info.generation == 10 && info.variant == 6)
        return UIDevice.isFullScreen() && (iphnex || info.generation > 10)
    }
    
    /// 判断是否为刘海屏机型（iPhone X ~ iPhone 14 普通系列）
    static func isNotchScreen() -> Bool {
        guard let info = UIDevice().getDeviceGeneration() else { return false }
        // 判断是否为全面屏且 非 刘海屏幕
        return UIDevice.isFullScreen() && info.generation <= 15
    }
    
    /// 判断是否为短刘海屏机型（iPhone X ~ iPhone 14 和 Plus）
    static func isShortNotchScreen() -> Bool {
        guard let info = UIDevice().getDeviceGeneration() else { return false }
        // 判断是否为全面屏且 非 刘海屏幕
        return isNotchScreen() && info.generation == 14
    }
    
    /// 灵动岛Dynamic Island
    static func isDynamicIsland() -> Bool {
        guard let info = UIDevice().getDeviceGeneration() else { return false }
        // 判断是否为全面屏且 非 刘海屏幕
        return UIDevice.isFullScreen() && info.generation >= 15
    }
    
    
    

    /// 提取设备代次数字（如 "iPhone15,4" → 15,4）
    private func getDeviceGeneration() -> (generation:Int,variant:Int)? {
        let identifier = getDeviceIdentifier()
        // 正则匹配格式：iPhone代次,变种（如 iPhone15,2）
        let pattern = "^iPhone(\\d+),(\\d+)$"
        print(identifier)
        guard let regex = try? NSRegularExpression(pattern: pattern),
              let match = regex.firstMatch(in: identifier, range: NSRange(identifier.startIndex..., in: identifier)),
              let genRange = Range(match.range(at: 1), in: identifier),
              let varRange = Range(match.range(at: 2), in: identifier),
              let generation = Int(String(identifier[genRange])),
              let variant = Int(String(identifier[varRange]))
        else {
            return nil
        }
        return (generation,variant)
    }

    /// 获取设备标识符（兼容真机和模拟器）
    private func getDeviceIdentifier() -> String {
        // 模拟器
        #if targetEnvironment(simulator)
            return ProcessInfo.processInfo.environment["SIMULATOR_MODEL_IDENTIFIER"] ?? ""
        #else
            var systemInfo = utsname()
            uname(&systemInfo)
            let machineMirror = Mirror(reflecting: systemInfo.machine)
            return machineMirror.children.reduce("") { id, element in
                guard let value = element.value as? Int8, value != 0 else { return id }
                return id + String(UnicodeScalar(UInt8(value)))
            }
        #endif
    }
}

